;(function(window) {
    window.FormBase = Base.extend({
        newValue: "",
        oldValue: "",
        info: "我是提示", //提示消息
        required: false, //是否必须
        checkImmd: false, //是否实时校验
        limitInput: true, //限制输入
        showError: true, //是否提示错误
        emptyMsg: "不能为空", //为空时提示消息
        formConfig: {},
        onlyBlurCheck: true,
        serviceConfig: {}, //后台校验信息
        mainContent: {},
        showInfoFocus: true, //focus 显示提示
        clearInfoBlur: false, //blur 清楚提示
        showErrorBlur: true, //blur 显示错误信息
        clearErrorFocus: true, //focus 清楚错误信息
        clearSusinfoFocus: true, //foucus 清除成功消息
        notCheckyet: true,
        blurFun: function() {
            //设置成功标志
            this.mainContent.susInfo(this.formEleId, "");
            $("#" + this.formEleId + this.mainContent.errPostfix).addClass("hide");
            $("#" + this.formEleId + this.mainContent.infoPostfix).addClass("hide");
        },
        focusFun: function() {
            if (this.clearSusinfoFocus) {
                this.mainContent.clearSusinfo(this.formEleId);
            }
        },
        constructor: function(formEleId, mainContent, formConfig, noListener) {
            this.formEleId = formEleId;
            this.formType = formConfig.formType;
            this.formEle = document.getElementById(formEleId);
            this.checkImmd = (formConfig && formConfig.checkImmd) ? true : false;
            this.emptyMsg = formConfig.emptyMsg ? formConfig.emptyMsg : this.emptyMsg;
            this.required = formConfig.required ? formConfig.required : false;
            this.info = formConfig.info ? formConfig.info : this.info;

            this.showInfoFocus = formConfig.showInfoFocus === false ? false: true;
            this.clearInfoBlur = formConfig.clearInfoBlur === true ? true: false;
            this.showErrorBlur = formConfig.showErrorBlur === false ? false: true;
            this.clearErrorFocus = formConfig.clearErrorFocus === false ? false: true;
            this.clearSusinfoFocus = formConfig.clearSusinfoFocus === false ? false: true;
            
            this.limitInput = formConfig.limitInput === false ? false : true;
            this.mainContent = mainContent;
            if (this.mainContent[this.mainContent.formId + "blurFun"][formEleId]) {
                this.blurFun = this.mainContent[this.mainContent.formId + "blurFun"][formEleId];
            }
            if (this.mainContent[this.mainContent.formId + "focusFun"][formEleId]) {
                this.blurFun = this.mainContent[this.mainContent.formId + "focusFun"][formEleId];
            }
            if (this.mainContent[this.mainContent.formId + "susFun"][formEleId]) {
                this.susFun = this.mainContent[this.mainContent.formId + "susFun"][formEleId];
            }
            if (this.mainContent[this.mainContent.formId + "errFun"][formEleId]) {
                this.errFun = this.mainContent[this.mainContent.formId + "errFun"][formEleId];
            }
            this.formConfig[formEleId] = formConfig;
            if (formConfig.serviceCheck) {
                this.hasServiceCheck = true;
                this.serviceConfig[formEleId] = formConfig.serviceCheck;
            }
            //初始化表单事件
            if (!noListener) this.initMouseListeners();
            //初始值
            if (formConfig && formConfig.initValue) this.formEle.value = formConfig.initValue;
            return this;
        },
        initMouseListeners: function() {
            if (document.all) { // IE
                this.formEle.attachEvent("onfocus", this.focusAction.bindAsEventListener(this));
                this.formEle.attachEvent("onblur", this.blurAction.bindAsEventListener(this));
                this.checkImmd && this.formEle.attachEvent("onpropertychange", this.changeAction.bindAsEventListener(this));
                //针对ie8实时处理
                this.checkImmd && this.isIE8() && this.formEle.attachEvent("onkeypress", this.keypressActionIE.bindAsEventListener(this));
                this.checkImmd && this.isIE8() && this.formEle.attachEvent("onkeyup", this.keyupActionIE.bindAsEventListener(this));
            } else {
                this.formEle.addEventListener("focus", this.focusAction.bindAsEventListener(this), false);
                this.formEle.addEventListener("blur", this.blurAction.bindAsEventListener(this), false);
                this.checkImmd && this.formEle.addEventListener("input", this.changeAction.bindAsEventListener(this), false);
            }
        },
        focusAction: function(e) {

            var config = JSON.parse($(e.target || e.srcElement).attr("form-base-config"));
            this.required = config.required === false ? false : true;
            this.formConfig[this.formEleId].required = config.required === false ? false : true;

            this.foucusClean = true;
            //清除错误信息
            var t2, tt = e.target || e.srcElement;
            if (tt) {
                t2 = $("#" + $(tt).attr("id") + this.mainContent.errPostfix);
                // if (this.clearErrorFocus && t2.length) t2.html("").addClass("hide");
                if (this.clearErrorFocus && t2.length) t2.addClass("hide");
            }
            //显示提示
            if (this.showInfoFocus) {
                this.mainContent.showInfo(this.formEleId, this.info);
            }
            this.focusFun();
        },
        blurAction: function(e) {

            this.blur = true;
            this.newValue = this.formEle.value;
            var result = this.handleResult();
            //是否显示错误信息{a. 没有service校验 且 b. showErrorBlur为 true 且 c. 不是空值且非必输 }

            //是否有service校验
            if (!this.hasServiceCheck || this.foucusClean) {
                if (!result && this.showErrorBlur && !(!this.newValue && !this.required)) {
                    //显示错误信息
                    $("#" + this.formEleId + this.mainContent.infoPostfix).addClass("hide");
                    this.mainContent.showErrors(this.formEleId);
                }
                if (result) this.blurFun();
            }
            //是否需要清除提示信息
            if (this.clearInfoBlur) {
                $("#" + this.formEleId + this.mainContent.infoPostfix).addClass("hide");
            }

            this.blur = false;

        },

        changeAction: function(e) {
            //如果IE8已经处理过了，跳过
            if (this.alreadyHandle) return;
            this.newValue = this.getEleValue(e);
            this.handleResult();
        },
        //针对IE8处理
        keypressActionIE: function(e) {
            var zz = /[0-9a-zA-Z.]*/;
            var txt = this.getCharFromKeyCode(e);
            if (zz.test(txt)) {
                this.alreadyHandle = true;
                this.newValue = this.oldValue + "" + txt;
                var result = this.handleResult({
                    isPress: true
                });
                if (result) {
                    this.oldValue = this.newValue;
                } else {
                    if (this.limitInput) {

                        return false;
                    }
                    this.oldValue = this.newValue;
                }
                return true;
            }
        },
        //针对IE8处理
        keyupActionIE: function(e) {
            this.alreadyHandle = false;
        },
        handleResult: function(options) {
            if (!options) options = {};
            var result = this.checkForm();
            if (this.newValue != this.oldValue) {
                this.notCheckyet = true;
            }
            if (!result.value && this.formConfig[this.formEleId].required && this.formConfig[this.formEleId].emptyMsg) {
                result.msg = this.formConfig[this.formEleId].emptyMsg;
            }
            if (!result.value && !this.formConfig[this.formEleId].required && !options.isPress) {
                result.status = true;
                this.oldValue = result.value
            }
            if (!result.status) {
                //presss事件 针对ie8处理
                if (options && options.isPress) {
                    this.newValue = result.value;
                    this.oldValue = result.value;
                    this.comErrFun();
                    return false;
                } else {
                    //添加到err中
                    this.mainContent[this.mainContent.formId + "errForm"][this.formEleId] = result;
                    //输入限制
                    if (this.limitInput) {
                        if (this.newValue === result.value) {
                            this.oldValue = this.newValue;
                            return;
                        }
                        this.formEle.value = result.value;
                        this.newValue = result.value;
                        this.handleResult();
                    } else {
                        this.comErrFun();
                    }
                }
            } else if (!this.hasServiceCheck || (this.hasServiceCheck && this.notCheckyet)) {
                if (options && options.isPress) {
                    this.oldValue = this.newValue;
                    this.comSusFun();
                    return true;
                } else {
                    if (this.formEle.value !== result.value) this.formEle.value = result.value;
                    //从err中删除
                    if (this.mainContent[this.mainContent.formId + "errForm"][this.formEleId])
                        delete this.mainContent[this.mainContent.formId + "errForm"][this.formEleId];
                    this.comSusFun();
                    this.oldValue = result.value;
                    if (this.hasServiceCheck) return false;
                    else return true;
                }
            }
        },
        checkForm: function() {},
        //获取事件元素的value
        getEleValue: function(e) {
            var tt = e.target || e.srcElement;
            return tt.value;
        },
        //获取事件keyCode对应的字符
        getCharFromKeyCode: function(e) {
            return String.fromCharCode(e.keyCode);
        },
        comSusFun: function() {
            if (this.newValue != this.oldValue) {
                //需要服务校验
                if (this.blur && this.hasServiceCheck && this.newValue) {
                    this.serviceCheck();
                } else {
                    this.susFun();
                }
            }
        },

        serviceCheck: function() {
            var serviceConfig = this.formConfig[this.formEleId].serviceCheck;
            var _this = this;
            var sendData = this.getSendData();
            _this.notCheckyet = false;
            this.doPost(sendData.url, sendData.data, {
                success: function(data) {
                    if (data.head.returnCode == "OK") {
                        _this.blurFun();
                        _this.susFun(data);
                    } else {
                        _this.mainContent[_this.mainContent.formId + "errForm"][_this.formEleId] = {
                            status: false,
                            msg: data.head.returnMsg,
                            value: ""
                        };
                        _this.comErrFun(data);
                    }
                },
                error: function(data) {
                    // _this.comErrFun();
                }
            });
        },
        comErrFun: function() {
            //提示错误
            if (this.showError) {
                this.foucusClean = false;
                if (this.mainContent[this.mainContent.formId + "errForm"][this.formEleId]) {
                    this.mainContent.showErrors(this.formEleId, true);
                    $("#" + this.formEleId + this.mainContent.infoPostfix).addClass("hide");
                }
            }
            this.errFun();
        },
        susFun: function() {},
        errFun: function() {},
        isIE8: function() {
            var browser = navigator.appName
            var b_version = navigator.appVersion
            var version = b_version.split(";");
            var trim_Version = version[1] ? version[1].replace(/[ ]/g, "") : '';
            if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE8.0") {
                return true;
            } else {
                return false;
            }
        },
        doPost: function(url, sendParam, callbacks) {
            $.ajax({
                url: url,
                type: "POST",
                dataType: "json",
                data: sendParam,
                success: callbacks.success,
                error: callbacks.error
            });
        },
        getMsg: function(msgKey) {
            return this.msgObj[msgKey] ? this.msgObj[msgKey] : (msgKey ? msgKey : '');
        },
        getSingleConfig: function() {
            return this.singleConfig[this.formEleId] ? this.singleConfig[this.formEleId] : {};
        },
        getSendData: function() {
            var data, url,
                submitConfig = this.serviceConfig[this.formEleId];
            url = submitConfig.url;
            data = this.getCurrentValue(submitConfig.data);
            return {
                url: url,
                data: data
            };
        },
        getCurrentValue: function(submitData) {
            var c, data = {};
            for (var key in submitData) {
                c = submitData[key];
                if (typeof c == "string") {
                    if (c.indexOf('#') === 0 && $(c).length)
                        data[key] = $(c).val();
                } else {
                    data[key] = this.getCurrentValue(c);
                }
            }
            return data;
        }
    });
})(window)
